<template>
  <div class="top-category">
    <div class="container">
      <!-- 安装Element-plus后测试是否可用 -->
      <!-- <el-button type="primary">我是按钮</el-button> -->
      <el-breadcrumb separator=">" style="height: 50px; line-height: 50px">
        <el-breadcrumb-item class="breadcrumb1" :to="{ path: '/' }"
          >首页</el-breadcrumb-item
        >
        <el-breadcrumb-item
          ><a href="/">{{ category.topCategory.name }}</a></el-breadcrumb-item
        >
      </el-breadcrumb>

      <el-carousel height="500px">
        <el-carousel-item v-for="item in home.bannerList" :key="item.id">
          <!-- <h3 class="small justify-center" text="2xl">{{ item }}</h3> -->
          <img :src="item.imgUrl" alt="" />
        </el-carousel-item>
      </el-carousel>

      <!-- 所有二级分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in category.topCategory.children" :key="item.id">
            <RouterLink :to="`/category/sub/${item.id}`">
              <img :src="item.picture" />
              <p>{{ item.name }}</p>
            </RouterLink>
          </li>
        </ul>
      </div>

      <!-- 二级分类的商品列表 -->
      <div
        class="ref-goods"
        v-for="item in category.topCategory.children"
        :key="item.id"
      >
        <div class="head">
          <h3>{{ item.name }}</h3>
          <XtxMore />
        </div>
        <!-- 父子传值中，父组件的 :goods="goods" 前一个goods为子组件 defineProps 传的 goods，后一个goods 是在 v-for="goods in item.goods" 中遍历的每一项goods -->
        <div class="body">
          <GoodsItem
            v-for="goods in item.goods"
            :key="goods.id"
            :goods="goods"
          />
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { watch, onMounted } from "vue";
import { useRoute } from "vue-router";
import useStore from "@/store";
import GoodsItem from "./components/GoodsItem.vue";

const { category, home } = useStore();
const route = useRoute();

onMounted(() => {
  home.getBannerList();
});

/**
 * 如果要监听对象的某个属性发生改变，写成箭头函数
 */
watch(
  () => route.params.id,
  () => {
    if (!route.params.id) return;

    // 滚动到顶部
    window.scroll({ top: 0 });

    console.log("---id---", route.params.id);
    console.log("---发请求---");
    category.getTopCategory(route.params.id as string);
  },
  {
    immediate: true,
  }
);
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: flex-start;
      flex-wrap: wrap;
      padding: 0 65px 30px;
    }
  }

  // .breadcrumb1 {
  //   font-weight: 400 !important;
  // }
}

// 取消字体的默认加粗

:deep(.el-breadcrumb__inner.is-link) {
  font-weight: 400;
}

:deep(.el-carousel__indicator--horizontal .el-carousel__button) {
  width: 12px;
  height: 12px;
  background: rgba(0, 0, 0, 0.2);
  border-radius: 50%;
  opacity: 0.5;
}

:deep(.el-carousel__indicator--horizontal.is-active .el-carousel__button) {
  width: 12px;
  height: 12px;
  background: #ffffff;
  border-radius: 50%;
  opacity: 1;
}
</style>
